/**
 * HTMLElementStyle 处理助手
 * @version:$Id: UIUtil.js 37 2008-03-02 17:24:56Z dsonet $
 */
/**
 * UIUtil 对象
 * @public
 */
var UIUtil =
{
  /**
   * 使用遮罩
   * @public
   * @member StyleUtil
   * @static
   * @return <HTMLElement> 遮罩内容层。是一个大小、位置始终与窗口匹配的空层。返回这个层，便于用户在window可见区域放置内容。
   */
	showMask: function()
	{
		this.hideMask();
		var container = document.getElementById(maskContainerId);

		if (container == null)
		{
			container = document.createElement("div");
			document.body.appendChild(container);
			container.id = maskContainerId;
			var masker = document.createElement("div")
				, content = document.createElement("div")
				, maskStyle = masker.style
				, contentStyle = content.style;
			container.appendChild(masker);
			container.appendChild(content);

			if (System.isIE())
			{
				maskStyle.background = "#CCCCCC";
				maskStyle.filter = "Alpha(opacity=50)";
			}
			else
			{
				maskStyle.backgroundImage = "url()";
			}
			contentStyle.position = maskStyle.position = "absolute";
			maskStyle.zIndex = 999;
			contentStyle.zIndex = 1000;
		}
		container.style.display = "block";

		Event.on(window, "resize", maskOverlay);
		Event.on(window, "scroll", maskOverlay);

		maskOverlay();
		return container.lastChild;
	},
  /**
   * 取消遮罩
   * @public
   * @member StyleUtil
   * @static
   */
	hideMask: function()
	{
		var container = document.getElementById(maskContainerId);
		if (container)
		{
			Event.un(window, "resize", maskOverlay);
			Event.un(window, "scroll", maskOverlay);
			container.style.display = "none";
		}
	},
	/**
	 * 设置透明度
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	setOpacity: function(node, opacity)
	{
		if (System.isIE())
		{
			if (node.nodeName.toLowerCase() == "tr")
			{
				// FIXME: is this too naive? will we get more than we want?
				var tds = node.getElementsByTagName("td");
				for (var x = 0; x < tds.length; x++)
				{
					tds[x].style.filter = "Alpha(Opacity=" + opacity * 100 + ")";
				}
				tds = null;
			}
			node.style.filter = "Alpha(Opacity=" + opacity * 100 + ")";
			return;
		}
		else if (System.isMozilla())
		{
			// ffox 1.0 directly supports "opacity"
			node.style.MozOpacity = opacity;
		}
		else if (System.isSafari())
		{
			// 1.3 directly supports "opacity"
			node.style.KhtmlOpacity = opacity;
		}
		node.style.opacity = opacity;
	}
	, 
	/**
	 * 获取元素当前透明度
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	getOpacity: function(node)
	{
		var fl,opac;
		if (System.isIE())
		{
			fl = node.filters;
			opac = (fl && fl.alpha && typeof fl.alpha.opacity == "number" ? fl.alpha.opacity : 100) / 100;
		}
		else
		{
			fl = node.style;
			opac = fl.opacity || fl.MozOpacity || fl.KhtmlOpacity || 1;
		}
		return opac >= 0.999999 ? 1.0 : opac; //	float
	}
	, 
	/**
	 * 获取滚动条的顶端位置
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	getScrollTop: function(element)
	{
		return getAttr(element, "scrollTop");
	}
	, 
	/**
	 * 获取滚动条的左端位置
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	getScrollLeft: function(element)
	{
		return getAttr(element, "scrollLeft");
	}
	, 
	/**
	 * 获取滚动条高度
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	getScrollHeight: function(element)
	{
		return getAttr(element, "scrollHeight");
	}
	, 
	/**
	 * 获取滚动条宽度
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	getScrollWidth: function(element)
	{
		return getAttr(element, "scrollWidth");
	}
	, 
	/**
	 * 获取窗口可见区域高度
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	getWindowHeight: function()
	{
		if (window.innerHeight >= 0)
		{
			return window.innerHeight;
		}
		if (document.compatMode == "CSS1Compat")
		{
			return document.documentElement.clientHeight;
		}
		if (document.body)
		{
			return document.body.clientHeight;
		}
	}
	, 
	/**
	 * 获取窗口可见区域宽度
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	getWindowWidth: function()
	{
		if (window.innerWidth >= 0)
		{
			//!= undefined
			return window.innerWidth;
		}
		if (document.compatMode == "CSS1Compat")
		{
			return document.documentElement.clientWidth;
		}
		if (document.body)
		{
			return document.body.clientWidth;
		}
	}
	, 
	/**
	 * 给指定元素设置class 属性
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	setClass: function(node, className)
	{
		node.className = className;
	}
	, 
	/**
	 * 给指定元素添加class 属性
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	addClass: function(node, className)
	{
		this.removeClass(node, className);
		node.className += (" " + className);
	}
	, 
	/**
	 * 给指定元素移除class 属性
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	removeClass: function(node, className)
	{
		var oldName = node.className;
		if (oldName.indexOf(className) >= 0)
		{
			node.className = oldName == className ? "" : oldName.replace(new RegExp("(^|\\s+)" + className + "(\\s+|$)"), " ");
		}
	}
	, 
	/**
	 * 给指定元素设置style文本属性
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	setStyleText: function(node, text)
	{
		if (typeof node.style.cssText == "string")
		{
			node.style.cssText = text;
		}
		else
		{
			node.setAttribute("style", text);
		}
	}
	, 
	/**
	 * 给指定元素设置style属性集
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	setStyleMap: function(node, valueMap)
	{
		node = node.style;
		for (var n in valueMap)
		{
			node[n] = valueMap[n];
		}
	}
	, 
	/**
	 * 设置元素是否可选择
	 * @public
	 * @member StyleUtil
	 * @static
	 */
	setSelectable: function(element, selectable)
	{
		// summary: disable selection on a node
		if (System.isGecko())
		{
			element.style.MozUserSelect = selectable ? "" : "none";
		}
		else if (System.isKhtml())
		{
			element.style.KhtmlUserSelect = selectable ? "" : "none";
		}
		else if (System.isIE())
		{
			selectable ? element.detachEvent("onselectstart", Type.F_FUNC) : element.attachEvent("onselectstart", Type.F_FUNC);
		}
		else
		{
			return false;
		}
		return true;
	}
};

/**
 * 页面显示方面的功能函数集，目前还不成熟，现在放js包里，只是觉得这个东西比较常用。
 * @public
 */
function getAttr(element, attribute)
{
	return parseInt(element? element[attribute] : (document.documentElement[attribute] || document.body[attribute]), 10);
}

var maskContainerId = System.PUID();

/*
 * @internal
 */
function maskOverlay()
{
	// Get the dimensions of the client area of the browser
	// We need to take into account the current scroll position of the window
	// Position the disabling DIV so that it exactly covers
	// the current client area of the browser
	var masker = document.getElementById(maskContainerId).firstChild
		, style = masker.style;
	style.top = "0px";
	style.left = "0px";
	style.height = UIUtil.getScrollHeight() + "px";
	style.width = UIUtil.getScrollWidth() + "px";

	style = masker.nextSibling.style;
	style.top = UIUtil.getScrollTop() + "px";
	style.left = UIUtil.getScrollLeft() + "px";
	style.height = UIUtil.getWindowHeight() - 20 + "px";
	style.width = UIUtil.getWindowWidth() - 20 + "px";
}